package com.czkj.hwz.decorate.Activity;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.czkj.hwz.decorate.R;
import com.czkj.hwz.decorate.adapter.SupervisorAdapter;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Enterprise;
import com.czkj.hwz.decorate.bean.JsonsRootBean;
import com.czkj.hwz.decorate.bean.MyTab;
import com.czkj.hwz.decorate.bean.Rows;
import com.google.gson.Gson;
import com.king.base.BaseActivity;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 监理页面
 * @author lxm
 * created at 2017/10/19 14:27
 */
public class SupervisorActivity extends BaseActivity {
    private static final String TAG = "SupervisorActivity";
    private LinearLayout tab_index;
    private MyTab mMyTab;
    private ListView lv;
    private ArrayList<Enterprise> mEnterprises;
    private List<Rows> rowsList;
    private SupervisorAdapter supervisorAdapter;
    private Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    supervisorAdapter.notifyDataSetChanged();
                    dismissProgressDialog();
                    break;
            }
        }
    };
    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= 21){
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
            window.setStatusBarColor(getResources().getColor(R.color.yellow));
        }
        setContentView(R.layout.activity_supervisor);
        getSupportActionBar().hide();
        showProgressDialog();
        tab_index=findView(R.id.tab_index);
        lv=findView(R.id.lv);
        mMyTab=new MyTab(context,LinearLayout.HORIZONTAL) {
            @Override
            public void onItemClickListener(int poistion) {
                getList(mEnterprises.get(poistion).getId());
            }
        };
        mEnterprises= (ArrayList<Enterprise>) getIntent().getSerializableExtra("mEnterprises");
        for (int i=0;i<mEnterprises.size();i++) {
            Log.d(TAG, "initUI: "+mEnterprises.get(i).toString());
            Log.d(TAG, "initUI: "+mEnterprises.get(i).getAreaCode());
            Log.d(TAG, "initUI: "+mEnterprises.get(i).getId());
        }
        rowsList=new ArrayList<Rows>();
        supervisorAdapter=new SupervisorAdapter(context,rowsList);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(SupervisorActivity.this,SupervisorDetailActivity.class);
                intent.putExtra("rowsList", (Serializable) rowsList);
                intent.putExtra("position",position);
                startActivity(intent);

            }
        });
    }

    @Override
    public void initData() {
        mMyTab.setData(mEnterprises);
        tab_index.addView(mMyTab.getView());
        if (mMyTab!=null) {
            mMyTab.changeColor(0);
        }
        getList(mEnterprises.get(0).getId());
        lv.setAdapter(supervisorAdapter);

    }

    private void getList(final int enterpriseid) {
      new Thread(new Runnable() {
          @Override
          public void run() {

              try {
                  OkHttpClient client=new OkHttpClient();
                  RequestBody requestBody=new FormBody.Builder().add("enterpriseid",String.valueOf(enterpriseid)).build();
                  Request request=new Request.Builder().url(HttpUrl.SUPERVISOR_SORT).post(requestBody).build();
                  Response response=client.newCall(request).execute();
                  String responseDta=response.body().string();
                  parseJson(responseDta);
              } catch (IOException e) {
                  e.printStackTrace();
              }


          }
      }).start();

    }

    private  void parseJson(String responseData) {
        rowsList.clear();

        Gson gson=new Gson();
        JsonsRootBean jsonsRootbean=gson.fromJson(responseData,JsonsRootBean.class);
        Log.d(TAG, "parseJson: "+jsonsRootbean.getRows().toString());
        Log.d(TAG, "parseJson: "+jsonsRootbean.getTotal());
        for (int i=0;i<jsonsRootbean.getRows().size();i++) {
            Rows rows = jsonsRootbean.getRows().get(i);
            rowsList.add(rows);
        }
        handler.sendEmptyMessage(0);
    }


    @Override
    public void addListeners() {

    }
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_search:
                startActivity(SupervisorSearchActivity.class);
                break;
        }
    }
}
